/**
 * 测试用例执行结果赋给预期值测试用例
 *
 * Created by liuxuechao on 2022/08/15.
 */
const chai = require('chai');
const should = chai.should(); // eslint-disable-line no-unused-vars
chai.use(require('chai-http'));

const common = require('./../../common');
const Administrator = require('./../../../models/administrator');
const uri = require('./../uri');
const benchmarks = [require('./../../../benchmark/run-tests/self_designed')];

module.exports = describe('/PUT :testId/output/result-copy.json', function () {
  const administrator = new Administrator();
  let project = {};
  const newUserIds = [];
  function url(testId) {
    return `/${testId}/output/result-copy.json`;
  }

  before(function (done) {
    administrator.login()
      .then(() => administrator.createRandomTester())
      .then((tester) => {
        newUserIds.push(tester.userId);
        return tester.createRandomProject();
      })
      .then((res) => {
        project = res;
        done();
      })
      .catch(done);
  });
  after(function (done) {
    administrator.removeUsers(newUserIds)
      .then(() => {
        done();
      })
      .catch(done);
  });

  common.should.be.user(uri, url, 'put');
  common.should.be.tester(uri, url, 'put');

  // 正常发起请求将测试用例实际运行结果赋给预期值
  benchmarks.forEach((benchmark) => {
    it(`it should copy operation result of a test in ${benchmark.name}`, function (done) {
      let projectVersion;
      let functionModel;
      let testModel;
      project.createRandomVersion()
        .then((res) => {
          projectVersion = res;
          return projectVersion.getAllFunctions(benchmark);
        })
        .then((res) => {
          [functionModel] = res;
          return functionModel.rootDirectory.generateTests([functionModel.functionId]);
        })
        .then(() => functionModel.getTests())
        .then((res) => {
          [testModel] = res;
          // functionModel = testModel.function;
          return project.creator.login();
        })
        // run tests of functionModel
        .then(() => projectVersion.runFunctionTests([functionModel.functionId]))
        // copy result
        .then(() => {
          chai.request(uri)
            .put(url(testModel.testId))
            .set('Authorization', `Bearer ${project.creator.token.accessToken}`)
            .end((err, res) => {
              res.should.have.status(200);
              done();
            });
        })
        .catch(done);
    });
  });
});
